﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;

namespace QLCF.Forms
{
    public partial class frmTable : Form
    {
        public int permission = 0;
        public frmTable()
        {
            InitializeComponent();
            permission = CtrAdmin.GetPermission(Program.UserEntity.Permission, Config.Enum.Function.frmTable);               
        }
        private int tableID = -1;
        private int rangeID = -1;
        private bool isNew = false;
        private DataGridView grvCur = null;
        private void frmTable_Load(object sender, EventArgs e)
        {            
            tabControl.Dock = DockStyle.Fill;
            CtrTable ctrT=new CtrTable();
            var lastNameList = ctrT.RangeGetAll();
            foreach (var item in lastNameList)
            {
                TabPage tabPage = new TabPage();
                tabPage.Name = "tabPage" + item.RangeID;                
                tabPage.Text = item.RangeName;

                DataGridView gr1 = new DataGridView();
                gr1.Name = "grvTable" + item.RangeID;
                gr1.AutoGenerateColumns = false;
                gr1.MultiSelect = false;
                gr1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                var col1 = new DataGridViewTextBoxColumn();

                col1.Name = "RowNumber";
                col1.DataPropertyName = "RowNumber";
                col1.HeaderText = "STT";
                col1.ReadOnly = true;
                col1.Width = 50;
                gr1.Columns.Add(col1);

                col1 = new DataGridViewTextBoxColumn();
                col1.Name = "TableCode";
                col1.DataPropertyName = "TableCode";
                col1.HeaderText = "Mã bàn";
                col1.ReadOnly = true;
                col1.Width = 80;
                gr1.Columns.Add(col1);

                col1 = new DataGridViewTextBoxColumn();
                col1.Name = "TableName";
                col1.DataPropertyName = "TableName";
                col1.HeaderText = "Tên bàn";
                col1.ReadOnly = true;
                col1.Width = 150;
                gr1.Columns.Add(col1);

                col1 = new DataGridViewTextBoxColumn();
                col1.Name = "StatusName";
                col1.DataPropertyName = "StatusName";
                col1.HeaderText = "Trạng thái";
                col1.ReadOnly = true;
                col1.Width = 100;
                gr1.Columns.Add(col1);

                col1 = new DataGridViewTextBoxColumn();
                col1.Name = "TableID";
                col1.DataPropertyName = "TableID";
                col1.HeaderText = "TableID";
                col1.ReadOnly = true;
                col1.Visible = false;
                gr1.Columns.Add(col1);

                col1 = new DataGridViewTextBoxColumn();
                col1.Name = "RangeID";
                col1.DataPropertyName = "RangeID";
                col1.HeaderText = "RangeID";
                col1.ReadOnly = true;
                col1.Visible = false;
                gr1.Columns.Add(col1);

                col1 = new DataGridViewTextBoxColumn();
                col1.Name = "Status";
                col1.DataPropertyName = "Status";
                col1.HeaderText = "Status";
                col1.ReadOnly = true;
                col1.Visible = false;
                gr1.Columns.Add(col1);

                gr1.Dock = DockStyle.Fill;

                gr1.RowEnter+=new DataGridViewCellEventHandler(gr1_RowEnter);

                gr1.DataSource= ctrT.TableGetByRangeID(item.RangeID, -1);
                gr1.BackgroundColor = Color.White;
                gr1.BorderStyle = BorderStyle.None;
                tabPage.Controls.Add(gr1);
                tabControl.Controls.Add(tabPage);
            }
            BindCbxStatus();
            BindCbxRange();
            DisplayControls(false);
        }
        private void BindCbxStatus()
        {
            cbxStatus.Items.Clear();
            cbxStatus.DisplayMember = "text";
            cbxStatus.ValueMember = "value";
            List<EntityBLL.ListItem> lst1 = new List<EntityBLL.ListItem>();
            lst1.Add(new EntityBLL.ListItem(1, "Hoạt động"));
            lst1.Add(new EntityBLL.ListItem(0, "Khóa"));
            cbxStatus.DataSource = lst1;
            cbxStatus.SelectedValue = 1;
        }
        private void BindCbxRange()
        {
            cbxRange.Items.Clear();
            CtrTable ctrT = new CtrTable();
            cbxRange.DataSource = ctrT.RangeGetAll();
            cbxRange.DisplayMember = "RangeName";
            cbxRange.ValueMember = "RangeID";                                     
            if(cbxRange.Items.Count>0)
                cbxStatus.SelectedIndex = 0;
        }
        private void gr1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (!isNew)
            {
                try
                {
                    var row = e.RowIndex;
                    if (row < 0)
                        return;
                    DataGridView gr1 = (DataGridView)sender;
                    grvCur = gr1;
                    rangeID=Convert.ToInt32(gr1.Rows[row].Cells["RangeID"].Value);
                    txtTableName.Text = gr1.Rows[row].Cells["TableName"].Value.ToString();
                    txtTableCode.Text = gr1.Rows[row].Cells["TableCode"].Value.ToString();
                    cbxStatus.SelectedValue = Convert.ToInt32(gr1.Rows[row].Cells["Status"].Value);
                    cbxRange.SelectedValue = rangeID;
                    tableID = Convert.ToInt32(gr1.Rows[row].Cells["TableID"].Value);
                }
                catch (Exception loi)
                {
                    MessageBox.Show(loi.Message.ToString());
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            cbxRange.SelectedValue = rangeID;
            grbEdit.Text = "Thêm bàn mới";
            txtTableName.Text = "";
            txtTableCode.Text = "";
            cbxStatus.SelectedValue = 1;
            tableID = -1;
            DisplayControls(true);
            btnSave.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            grbEdit.Text = "Sửa thông tin";
            DisplayControls(false);
            var ctrol=tabControl.SelectedTab.Controls.Find("grvTable"+rangeID, true);
            if (ctrol.Length >0)
            {
                DataGridView gr1 = null;
                try
                {
                    gr1 = (DataGridView)ctrol[0];
                }
                catch
                {
                    return;
                }
                if (gr1.SelectedCells.Count <= 0)
                    return;
                var row = gr1.SelectedCells[0].RowIndex;
                if (row < 0)
                    return;

                grvCur = gr1;
                rangeID = Convert.ToInt32(gr1.Rows[row].Cells["RangeID"].Value);
                txtTableName.Text = gr1.Rows[row].Cells["TableName"].Value.ToString();
                txtTableCode.Text = gr1.Rows[row].Cells["TableCode"].Value.ToString();
                cbxStatus.SelectedValue = Convert.ToInt32(gr1.Rows[row].Cells["Status"].Value);
                cbxRange.SelectedValue = rangeID;
                tableID = Convert.ToInt32(gr1.Rows[row].Cells["TableID"].Value);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (tableID < 0)
            {
                MessageBox.Show("Bạn chưa chọn bàn!", "Thông báo");
                return;
            }
            if (MessageBox.Show("Bạn có muốn xóa bàn này không?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                CtrTable ctrT = new CtrTable();
                int ret = ctrT.TableDelete(tableID);
                if (ret > 0)
                {
                    CtrAdmin ctra = new CtrAdmin();
                    ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 3, "Xóa bàn \"" + txtTableName.Text + "\" mã \"" + txtTableCode.Text + "\"", "Quản lý bàn");
                    grvCur.DataSource = ctrT.TableGetByRangeID(rangeID, -1);
                }
                else if (ret == -1)
                    MessageBox.Show("Không thể xóa bàn khi đã có đơn hàng","Thông báo");
                else
                    MessageBox.Show("Có lỗi xảy ra!", "Thông báo!");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int ret = 0;
            CtrTable ctrT = new CtrTable();
            if (txtTableCode.Text.Trim() == "")
            {
                MessageBox.Show("Bạn chưa nhập mã bàn!");
                txtTableCode.Focus();
                return;
            }
            if (txtTableName.Text.Trim() == "")
            {
                MessageBox.Show("Bạn chưa nhập tên bàn!");
                txtTableName.Focus();
                return;
            }
            if (isNew)
            {
                ret = ctrT.TableInsert(txtTableCode.Text,txtTableName.Text, Convert.ToInt32(cbxStatus.SelectedValue), rangeID);
                if (ret > 0)
                {
                    CtrAdmin ctra = new CtrAdmin();
                    ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 1, "Thêm bàn \"" + txtTableName.Text + "\" mã \"" + txtTableCode.Text + "\"", "Quản lý bàn");
                    grbEdit.Text = "Sửa thông tin";
                    DisplayControls(false);
                }                
            }
            else
            {
                int curRangeID=Convert.ToInt32(cbxRange.SelectedValue);
                ret = ctrT.TableUpdate(txtTableCode.Text, txtTableName.Text, Convert.ToInt32(cbxStatus.SelectedValue), curRangeID, tableID);
                if (ret > 0)
                {
                    CtrAdmin ctra = new CtrAdmin();
                    ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 2, "Sửa bàn \"" + txtTableName.Text + "\" mã \"" + txtTableCode.Text + "\"", "Quản lý bàn");
                }
                if ((ret > 0)&&(rangeID!=curRangeID))
                {
                    var controls = splitContainer1.Panel1.Controls.Find("grvTable" + curRangeID, true);
                    DataGridView grvOther = null;
                    if(controls.Length>0)
                        grvOther = (DataGridView)controls[0];
                    if (grvOther != null)
                    {
                        grvOther.DataSource = ctrT.TableGetByRangeID(curRangeID, -1); 
                    }
                }
            }
            if (ret > 0)
            {
                MessageBox.Show("Cập nhật thành công!");
                grvCur.DataSource = ctrT.TableGetByRangeID(rangeID, -1);
            }
            else
                switch (ret)
                {
                    case -1:
                        MessageBox.Show("Mã bàn đã tồn tại!");
                        txtTableCode.Focus();
                        break;
                    case -2:
                        MessageBox.Show("Tên bàn đã tồn tại ở khu vực này!");
                        txtTableName.Focus();
                        break;
                    default:
                        MessageBox.Show("Có lỗi xảy ra!");
                        break;
                }
        }
        

        private void DisplayControls(bool m_new)
        {
            isNew = m_new;
            btnAdd.Enabled = !m_new;
            btnCancel.Enabled = m_new;
            btnDelete.Enabled = !m_new;
            if ((permission & (int)Config.Enum.Permission.Add) != (int)Config.Enum.Permission.Add)
                btnAdd.Enabled = false;

            if ((permission & (int)Config.Enum.Permission.Edit) != (int)Config.Enum.Permission.Edit)
                btnSave.Enabled = false;

            if ((permission & (int)Config.Enum.Permission.Delete) != (int)Config.Enum.Permission.Delete)
                btnDelete.Enabled = false;
        }
    
        
    }
}
